﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/3/31 16:46:33
*
* */
namespace Novel.Sys.Dal
{
    public class novel_session
    {

        /// <summary>
        /// 验证是否登录
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Passwd"></param>
        /// <returns></returns>
        public bool Exists(int Uid, string SessionKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_session]");
            strSql.Append(" where uid=@uid and session=@session");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int),
                    new SqlParameter("@session", SqlDbType.NVarChar,60)};
            parameters[0].Value = Uid;
            parameters[1].Value = SessionKey;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="Session"></param>
        /// <param name="Created"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public int Add(Model.novel_session model)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO [novel_session] ([uid], [session], [created], [endtime]) VALUES (")
              .Append("@uid,@session, @created, @endtime);select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int),
                    new SqlParameter("@session", SqlDbType.NVarChar,60),
                    new SqlParameter("@created", SqlDbType.DateTime),
                    new SqlParameter("@endtime", SqlDbType.DateTime) };
            parameters[0].Value = model.Uid;
            parameters[1].Value = model.Session;
            parameters[1].Value = model.Created;
            parameters[1].Value = model.Endtime;
            object obj= DbHelpers.GetSingle(sb.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 删除一个
        /// </summary>
        /// <param name="Uid"></param>
        public void Del(int Uid)
        {
            string sql = "DELETE TOP(1) FROM [novel_session] WHERE ([uid]='"+Uid+"') ";
            DbHelpers.NonQuery(sql);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Model.novel_session GetModel(int uid)
        {
            Model.novel_session model = new Model.novel_session();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [novel_session] where uid=@uid");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int)};
            parameters[0].Value = uid;
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
                {
                    model.Uid = int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["session"] != null && ds.Tables[0].Rows[0]["session"].ToString() != "")
                {
                    model.Session = ds.Tables[0].Rows[0]["session"].ToString();
                }
                if (ds.Tables[0].Rows[0]["created"] != null && ds.Tables[0].Rows[0]["created"].ToString() != "")
                {
                    model.Created = DateTime.Parse(ds.Tables[0].Rows[0]["created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["endtime"] != null && ds.Tables[0].Rows[0]["endtime"].ToString() != "")
                {
                    model.Endtime = DateTime.Parse(ds.Tables[0].Rows[0]["endtime"].ToString());
                }
                return model;
            }
            else
                return null;
        }


        public bool Exists(int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_session]");
            strSql.Append(" where uid=@uid");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int)};
            parameters[0].Value = uid;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }

        public void UpdateSession(int uid, string  session,DateTime endtime)
        {
            string sql = "UPDATE  [novel_session] SET [session]='" + session + "', [endtime]='" + endtime + "' WHERE [uid]='"+uid+"'";
            DbHelpers.NonQuery(sql);
        }
    }

}

